package jwdt.web.action;

import jwdt.xadmin.info.bean.TNews;
import jwdt.xadmin.system.bean.TSystem;

import com.ezerg.db.Jdb;
import com.ezerg.web.Form;
import com.ezerg.web.IAction;
import com.ezerg.web.Module;
import com.ezerg.web.Page;

public class BaseAction implements IAction {
	
	public final String select_news = " SELECT id,title,titlecolor,titleface,updatetime,picname,istop,goodnews,daodu,TO_DAYS(NOW())-TO_DAYS(updatetime)<3 AS n,content,editor,click,author,url FROM t_news ";
	public final String order_news = " ORDER BY istop DESC,Sequence DESC, updatetime DESC ";

	public Jdb jdb = Jdb.get();
	public TSystem system = (TSystem)jdb.get(TSystem.class, 1);
	
	public BaseAction() {
	}
	
	public Page execute(Form f, Module m) throws Exception {
		return m.defaultPage();
	}
	
	public void getListByTypeFid(int typefid, String name, int rows, Form f) {
		String sql = select_news + " WHERE typeid in(select id from t_type where fid= " + typefid + ") and checked=1 " + order_news + " limit " + rows;
		f.add(name, jdb.query(sql));
		//图片
		sql = select_news + " WHERE typeid in(select id from t_type where fid= " + typefid + ") and checked=1 and picnews=1" + order_news + " limit 1";
		f.add(name+"pic", jdb.queryDataMap(sql));
	}
	
	public void getListByTypeFid2(int typefid, int typeid2, String name, int rows, Form f) {
		String sql = select_news + " WHERE typeid in(select id from t_type where fid= " + typefid + ") and typeid2 = " + typeid2 + " and checked=1 " + order_news + " limit " + rows;
		f.add(name, jdb.query(sql));
		//图片
		sql = select_news + " WHERE typeid in(select id from t_type where fid= " + typefid + ") and typeid2 = " + typeid2 + " and checked=1 and picnews=1" + order_news + " limit 1";
		f.add(name+"pic", jdb.queryDataMap(sql));
	}
	
	public void getListByTypeid(int typeid, String name, int rows, Form f) {
		String sql = select_news + " WHERE typeid = " + typeid + " and checked=1 " + order_news + " limit " + rows;
		f.add(name, jdb.query(sql));
	}
	
	public void getListByTypeid2(int typeid, int typeid2, String name, int rows, Form f) {
		String sql = select_news + " WHERE typeid = " + typeid + " and typeid2 = " + typeid2 + " and checked=1 " + order_news + " limit " + rows;
		f.add(name, jdb.query(sql));
	}
	
	public TNews getDetailByTypeid(int typeid, String name, Form f) {
		TNews obj = null;
		int id = jdb.queryForInt("select id from t_news where typeid="+typeid+" order by updatetime desc limit 1");//简介
		if(id!=0) {
			obj = (TNews)jdb.get(TNews.class, id);
			try {
				jdb.execute("update t_news set click=click+1 where id="+id,false);
			} catch(Exception e){}
			obj.setClick(obj.getClick().intValue()+1);
			f.add(name, obj);
		}
		return obj;
	}
	
}
